package com.animal.controller;

import com.animal.common.Constants;
import com.animal.common.Result;
import com.animal.entity.pojo.AActivity;
import com.animal.entity.pojo.AComment;
import com.animal.service.CommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/comment")
@Controller
public class CommentController {

    @Autowired
    private CommentService commentService;

    /**
     * 评论列表
     * @return
     */
    @ResponseBody
    @PostMapping("/commentlist/{pageNo}/{pageSize}")
    public Result selectAllComment(@RequestBody(required = false) AComment comment,
                                   @PathVariable Integer pageNo,
                                   @PathVariable Integer pageSize
                                  ){
        List<AComment> list = commentService.selectAllComment(comment);
        PageHelper.startPage(pageNo, pageSize);
        PageInfo<AComment> pageInfo = new PageInfo<>(list);

        return Result.success(Constants.CODE_200,"",pageInfo);
    }

    /**
     * 查询某一条的信息
     * @param id
     * @return
     */
    @ResponseBody
    @PostMapping("/queryById/{id}")
    public Result queryById(@PathVariable("id") Integer id){
        AComment comment = commentService.queryById(id);
        return Result.success(Constants.CODE_200,"",comment);
    }

    /**
     * 保存评论
     * @param comment
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public Result save(@RequestBody AComment comment) {
        System.out.println(comment.getComment());
        int count = commentService.insertComment(comment);
        if (count>0){
            return Result.success(Constants.CODE_200,"添加成功",null);
        }else {
            return Result.error(Constants.CODE_500,"添加失败");
        }
    }

    /**
     * 修改评论
     * @param comment
     * @return
     */
    @ResponseBody
    @PostMapping("/updateComment")
    public Result updateActDescribe(@RequestBody AComment comment){
        int i = commentService.updateComment(comment);
        if (i>0){
            return Result.success(Constants.CODE_200,"修改成功",null);
        }else {
            return Result.error(Constants.CODE_500,"修改失败");
        }
    }

    /**
     * 删除单个
     * @param id
     * @return
     */
    @RequestMapping(value = "/deleteById/{id}",method = RequestMethod.POST)
    @ResponseBody
    public Result deleteById(@PathVariable("id") Integer id) {
        int flag = commentService.deleteById(id);
        if (flag > 0) {
            return Result.success(Constants.CODE_200,"删除成功",null);
        } else {
            return Result.error(Constants.CODE_500,"删除失败");
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delAll",method = RequestMethod.POST)
    public Result delAll(@RequestBody List<Integer> ids){

        for(Integer id:ids){
            commentService.deleteById(id);
            System.out.println(id);
        }
        return Result.success(Constants.CODE_200,"删除成功",null);
    }

}
